package com.wifeacountbook.util;

import com.wifeacountbook.util.AcountKeeper.ConsumeDetail;
import com.wifeacountbook.util.AcountKeeper.ConsumeType;
import com.wifeacountbook.util.AcountKeeper.IncomeDetail;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class AcountDBOpenHelper extends SQLiteOpenHelper {
	private static final String ACOUNT_TABLE_NAME_CONSUME_DETAIL = "acount_consume_info";
	private static final String ACOUNT_TABLE_NAME_CONSUME_TYPE = "acount_consume_type";
	private static final String ACOUNT_TABLE_NAME_INCOME_DETAIL = "acount_income_info";
	//ZH consume and income type
	private static final String[] INIT_CONSUME_TYPE_DATA = new String[]{"饮食","交通","休闲娱乐","服饰美容","教育","通讯","投资",
		"居住","医疗","学习","水电费","日用品","交际","其它"};
	private static final String[] INIT_INCOME_TYPE_DATA = new String[]{"工资","奖金","投资","积蓄","其它"};
	//en consume and income type
	private static final String[] INIT_CONSUME_TYPE_DATA_EN = new String[]{"Diet", "Traffic", "entertainment","Fashion Beauty", 
		"education ","communication ","investment", "residence ", "medical"," learning","utilities","commodity" , "communication","other "};
	private static final String[] INIT_INCOME_TYPE_DATA_EN = new String[]{"Wages", "bonus ", "investment", "savings","other "};
	
	private static final String DB_NAME = "acount_consume_note.db";
	private static final int DB_VERSION = 1;

	public AcountDBOpenHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create ACOUNT_TABLE_NAME_NOTE
		String sql = new StringBuilder().append("CREATE TABLE ").append(
				ACOUNT_TABLE_NAME_CONSUME_DETAIL).append("(").append(
				ConsumeDetail._ID).append(" INTEGER PRIMARY KEY,")
				.append(ConsumeDetail.CONSUMEITEM).append(" TEXT,")
				.append(ConsumeDetail.ADDRESS).append(" TEXT,").append(
				ConsumeDetail.CONSUMETIME).append(" INTEGER,").append(
				ConsumeDetail.CONSUMETYPEID).append(" INTEGER,").append(
				ConsumeDetail.DESC).append(" TEXT,").append(
				ConsumeDetail.PAYTYPE).append(" TEXT,").append(
				ConsumeDetail.MONEY).append(" TEXT,").append(
				ConsumeDetail.ACOUNTID).append(" INTEGER);").toString();
		db.execSQL(sql);
		// create ACOUNT_TABLE_NAME_INCOME_DETAIL
		sql = new StringBuilder().append("CREATE TABLE ").append(
				ACOUNT_TABLE_NAME_INCOME_DETAIL).append("(").append(
				IncomeDetail._ID).append(" INTEGER PRIMARY KEY,")
				.append(IncomeDetail.INCOMEITEM).append(" TEXT,")
				.append(IncomeDetail.ADDRESS).append(" TEXT,").append(
				IncomeDetail.INCOMETIME).append(" INTEGER,").append(
				IncomeDetail.INCOMETYPEID).append(" INTEGER,").append(
				IncomeDetail.DESC).append(" TEXT,").append(
				IncomeDetail.PAYTYPE).append(" TEXT,").append(
				IncomeDetail.MONEY).append(" TEXT,").append(
				IncomeDetail.ACOUNTID).append(" INTEGER);").toString();
		db.execSQL(sql);
		// create ACOUNT_TABLE_NAME_CONSUME_TYPE
		sql = new StringBuilder().append("CREATE TABLE ").append(ACOUNT_TABLE_NAME_CONSUME_TYPE).append("(")
		.append(ConsumeType._ID).append(" INTEGER PRIMARY KEY,").append(ConsumeType.TYPENAME).append(" TEXT,")
		.append(ConsumeType.TYPE).append(" INTEGER,")
		.append(ConsumeType.LANG).append(" TEXT,")
		.append(ConsumeType.CONSUME_TYPE_COLUMN1).append(" TEXT);").toString();
		db.execSQL(sql);
		//init data of ACOUNT_TABLE_NAME_CONSUME_TYPE
		for(String type:INIT_CONSUME_TYPE_DATA){
			ContentValues value = new ContentValues();
			value.putNull(ConsumeType.CONSUME_TYPE_COLUMN1);
			value.put(ConsumeType.TYPE, 0);
			value.put(ConsumeType.LANG, "zh");
			value.put(ConsumeType.TYPENAME, type);
			db.insert(ACOUNT_TABLE_NAME_CONSUME_TYPE, ConsumeType.TYPENAME, value);
		}
		
		for(String type:INIT_INCOME_TYPE_DATA){
			ContentValues value = new ContentValues();
			value.putNull(ConsumeType.CONSUME_TYPE_COLUMN1);
			value.put(ConsumeType.TYPE, 1);
			value.put(ConsumeType.LANG, "zh");
			value.put(ConsumeType.TYPENAME, type);
			db.insert(ACOUNT_TABLE_NAME_CONSUME_TYPE, ConsumeType.TYPENAME, value);
		}
		
		for(String type:INIT_CONSUME_TYPE_DATA_EN){
			ContentValues value = new ContentValues();
			value.putNull(ConsumeType.CONSUME_TYPE_COLUMN1);
			value.put(ConsumeType.TYPE, 0);
			value.put(ConsumeType.LANG, "en");
			value.put(ConsumeType.TYPENAME, type);
			db.insert(ACOUNT_TABLE_NAME_CONSUME_TYPE, ConsumeType.TYPENAME, value);
		}
		
		for(String type:INIT_INCOME_TYPE_DATA_EN){
			ContentValues value = new ContentValues();
			value.putNull(ConsumeType.CONSUME_TYPE_COLUMN1);
			value.put(ConsumeType.TYPE, 1);
			value.put(ConsumeType.LANG, "en");
			value.put(ConsumeType.TYPENAME, type);
			db.insert(ACOUNT_TABLE_NAME_CONSUME_TYPE, ConsumeType.TYPENAME, value);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//TODO
	}

	
	public Cursor query(String query)
	 {
	  Cursor cur = this.getReadableDatabase().rawQuery(query, null);
	  return cur;
	 }
}
